package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.service.TransportRPCService;
import id.net.iconpln.bb.client.service.TransportRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbPerhitunganStock extends LayoutContainer {
	private final MasterRPCServiceAsync serviceMaster = GWT.create(MasterRPCService.class);
	private final TransportRPCServiceAsync serviceTransport = GWT.create(TransportRPCService.class);
	private Grid grid, gridkit;
	private ListStore store, storePembangkit;
	private ColumnModel cm;
	private Button btnLoad;	
	private ToolBar toolbar;
	  
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
		add(createExpander(), new FitData(5));
	  }  
	  private ContentPanel createExpander() {  
		    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();      
		    store = new ListStore();	
		    
		    RowNumberer rn = new RowNumberer();  
		    rn.setWidth(30);
		    configs.add(rn);
		    
		    ColumnConfig column = new ColumnConfig();	 
		    column = new ColumnConfig("tanggal", "Tanggal", 100);  
		    configs.add(column);  
		    
		    column = new ColumnConfig("stok_awal", "Stok Awal", 100);  
		    configs.add(column); 

		    column = new ColumnConfig("jml_penerimaan", "Penerimaan", 100);  
		    configs.add(column);  	
		    
		    column = new ColumnConfig("jml_pemakaian", "Pemakaian", 100);  
		    configs.add(column);  
		    		  
		    column = new ColumnConfig("stok_akhir", "Stok Akhir", 100);  
		    configs.add(column);		    
		    
		    cm = new ColumnModel(configs);
		    
		    grid = new Grid(store, cm);  
		    grid.setStripeRows(true);
		    grid.setLoadMask(true);  
		    grid.mask("load Data ...","x-mask-loading");

	        
	        btnLoad = new Button("Refresh", new SelectionListener<ButtonEvent>() {  
			      @Override  
			      public void componentSelected(ButtonEvent ce) {
			    	  loadStock(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());    
			      }  
			    });
	        btnLoad.setIcon(IconHelper.createStyle("icon-refresh"));	        
		    toolbar = new ToolBar();	
		    toolbar.add(new LabelToolItem("<b>Informasi Stok</b>"));
		    toolbar.add(new SeparatorToolItem());
			toolbar.add(btnLoad);
			
			createGridkit();
			
		    ContentPanel cp = new ContentPanel();  
		    cp.setHeaderVisible(false);
		    cp.setBorders(false);
		    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));	  
		    
		    String level = Registry.get("levelUser");

		    ContentPanel cpShip = new ContentPanel();
		    cpShip.setHeading("Data Stok");
		    cpShip.setHeaderVisible(false);
		    cpShip.setBorders(false);
		    cpShip.setLayout(new FitLayout());

		    if (!level.equals("PLTU")){
			    ContentPanel cp1 = new ContentPanel();
			    cp1.setBorders(false);
			    cp1.setLayout(new FitLayout());
			    cp1.setHeading("Daftar Pembangkit");
			    cp1.add(gridkit);
			    
			    cp.add(cp1, new RowData(.40, .99));
			    ContentPanel delimeter = new ContentPanel();
			    delimeter.setHeaderVisible(false);
			    delimeter.setWidth(10);
			    cp.add(delimeter, new FitData(5));
			    
			    cpShip.add(grid);			    			   
			    cp.add(cpShip, new RowData(.60, .99));
		    }else{
		    	loadStock(Registry.get("idPltu").toString());
			    cpShip.add(grid);
			    cp.add(cpShip, new RowData(1, .99));
		    }
		    		    	
		    cpShip.setTopComponent(toolbar);
		  
		    return cp;  
	  }
	  
	  private void createGridkit() { 
			final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 	    
			storePembangkit = new ListStore();
			getdataKit(); 
			
			RowNumberer id = new RowNumberer();  
		    id.setWidth(30);
		    
		    ColumnConfig nmkit = new ColumnConfig("nama_pembangkit", "Nama Pembangkit", 250);
		    nmkit.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(nmkit);
		    
		    final ColumnModel cm = new ColumnModel(columns);
		    gridkit = new Grid(storePembangkit, cm);
		    gridkit.addListener(Events.CellClick, new Listener<BaseEvent>() {            
	            public void handleEvent(BaseEvent ge) {
	            	loadStock(gridkit.getSelectionModel().getSelectedItem().get("id_pltu").toString());    
	            }
	        });
		}
	  
	  private void getdataKit() {
			serviceMaster.getMaster("KIT", new FastMap(), new AsyncCallback<List<Map>>() {
				public void onFailure(Throwable caught) {
					GWT.log(caught.getMessage(), null);
				}

				public void onSuccess(List<Map> result) {	
					storePembangkit.removeAll();
					storePembangkit.add(ClientModule.setListBaseModelFromListMap(result));
					
				}
			});
		};
	  
	  private void loadStock(String id_pltu){
		  Map map = new FastMap();
		  map.put("id_pltu",id_pltu);
		  serviceTransport.getListPemakaianBB("LIST_STOK", map, new AsyncCallback<List<Map>>() {
				
				@Override
				public void onSuccess(List<Map> result) {
					// TODO Auto-generated method stub
					store.removeAll();
					store.add(ClientModule.setListBaseModelFromListMap(result));
					grid.unmask();
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					GWT.log(caught.getMessage());
				}
			});
	  }
}